<?php 
$db = mysql_connect('localhost','root','') or die('can not connect to database');

mysql_select_db('chutest',$db) or die(mysql_error($db));
//上传文件的路径

$dir='upload/';
/*
$_FILES:用在当需要上传二进制文件的地方,获得该文件的相关信息
$_FILES['userfile']['name'] 客户端机器文件的原名称。 
$_FILES['userfile']['type'] 文件的 MIME 类型，需要浏览器提供该信息的支持，例如“image/gif” 
$_FILES['userfile']['size'] 已上传文件的大小，单位为字节
$_FILES['userfile']['tmp_name'] 文件被上传后在服务端储存的临时文件名,注意不要写成了$_FILES['userfile']['temp_name']很容易写错的，虽然tmp就是代表临时的意思，但是这里用的缩写
$_FILES['userfile']['error'] 和该文件上传相关的错误代码。['error'] 
*/
if($_FILES['uploadfile']['error'] != UPLOAD_ERR_OK)
{
    switch($_FILES['uploadfile']['error'])
    {
        case UPLOAD_ERR_INI_SIZE: //其值为 1，上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值
            die('The upload file exceeds the upload_max_filesize directive in php.ini');
        break;
        case UPLOAD_ERR_FORM_SIZE: //其值为 2，上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值
            die('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.');
        break;
        case UPLOAD_ERR_PARTIAL: //其值为 3，文件只有部分被上传
            die('The uploaded file was only partially uploaded.');
        break;
        case UPLOAD_ERR_NO_FILE: //其值为 4，没有文件被上传
            die('No file was uploaded.');
        break;
        case UPLOAD_ERR_NO_TMP_DIR: //其值为 6，找不到临时文件夹
            die('The server is missing a temporary folder.');
        break;
        case UPLOAD_ERR_CANT_WRITE: //其值为 7，文件写入失败
            die('The server failed to write the uploaded file to disk.');
        break;
        case UPLOAD_ERR_EXTENSION: //其他异常
            die('File upload stopped by extension.');
        break;
    }
}


$image_date = date('Y-m-D');
/*getimagesize方法返回一个数组，
$width : 索引 0 包含图像宽度的像素值，
$height : 索引 1 包含图像高度的像素值，
$type : 索引 2 是图像类型的标记：
1 = GIF，2 = JPG， 3 = PNG， 4 = SWF， 5 = PSD， 6 = BMP， 
7 = TIFF(intel byte order)，8 = TIFF(motorola byte order)，
9 = JPC，10 = JP2，11 = JPX，12 = JB2，13 = SWC，14 = IFF，15 = WBMP，16 = XBM，
$attr : 索引 3 是文本字符串，内容为“height="yyy" width="xxx"”，可直接用于 IMG 标记
*/

list($width,$height,$type,$attr) = getimagesize($_FILES['uploadfile']['tmp_name']);

//imagecreatefromgXXX方法从一个url路径中创建一个新的图片
switch($type)
{
    case IMAGETYPE_GIF:
        $image = imagecreatefromgif($_FILES['uploadfile']['tmp_name']) or die('The file you upload was not supported filetype');
        $ext = '.gif';
    break;
    case IMAGETYPE_JPEG:
        $image = imagecreatefromjpeg($_FILES['uploadfile']['tmp_name']) or die('The file you upload was not supported filetype');
        $ext = '.jpg';
    break;    
    case IMAGETYPE_PNG:
        $image = imagecreatefrompng($_FILES['uploadfile']['tmp_name']) or die('The file you upload was not supported filetype');
        $ext = '.png';
    break;    
    default    :
        die('The file you uploaded was not a supported filetype.');
}

//$imagename="xx.jpg";

//$file_name=$_FILES["uploadfile"]["name"];
//生成唯一ID
$s = md5(time() . mt_rand(1,1000000));
$r = substr($s, 0,8).'-'.substr($s,8, 4).'-'.substr($s,12, 4).'-'.substr($s,16, 4).'-'.substr($s,20, 32);

$file_name=strtolower ($r).$ext;
$img_url=$dir.$file_name;
$img_name=$_POST['ImageName'];
$img_type=$_POST['ImageType'];

$count="select count(*) as count from chu_image where ImageType='".$img_type."'";
$res=mysql_query($count , $db) or die(mysql_error($db));
if($res)
{
    $row=mysql_fetch_array($res);
    $sort=$row[0];
}

//echo $row;


$token=$_COOKIE["CCUSER"];
$uid="SELECT uid from chu_user where token='".$token."'";
$res1=mysql_query($uid , $db) or die(mysql_error($db));
if($res1)
{
    $row1=mysql_fetch_array($res1);
    $id=$row1[0];
}


$query = 'insert into chu_image(ImageUrl,ImageName,ImageType,Sort,UploaderId) values ("'.$img_url.'","'.$img_name.'","'.$img_type.'","'.$sort.'","'.$id.'")';
mysql_query("SET CHARACTER SET utf8");

mysql_query($query , $db) or die(mysql_error($db));
$last_id = mysql_insert_id();
//用写入的id作为图片的名字，避免同名的文件存放在同一目录中
$imagename = $last_id.$ext;
//$query = 'update chu_image set ImageName="'.$imagename.'" where Id='.$last_id;
//mysql_query($query , $db) or die(mysql_error($db));


//有url指定的图片创建图片并保存到指定目录
$res="上传成功";

    switch($type)
{
    case IMAGETYPE_GIF:
        imagegif($image,$dir.'/'.$file_name);
    break;
    case IMAGETYPE_JPEG:
        imagejpeg($image,$dir.'/'.$file_name);
    break;
    case IMAGETYPE_PNG:
        imagepng($image,$dir.'/'.$file_name);
    break;
}


//销毁由url生成的图片
imagedestroy($image);
?>
<html>
    <body style="margin: 0;">
        <label><?php echo $res ?></label>
    </body>
</html>